{
 "cells": [
  {
   "cell_type": "heading",
   "metadata": {},
   "level": 4,
   "source": [
    "pseudo vna scan"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "vcoil.ramp_to(0,steps=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "vcoil.get_current()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qubit.update_instruments()\n",
    "qubit.qubit_mw_src.set_status(0)\n",
    "load_awg.update_sequence([qubit.exc_T*.9],gwf.square,qubit)\n",
    "mspec.spec_stop()\n",
    "mspec.set_segments(1)\n",
    "mspec.set_blocks(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mspec.set_averages(5e3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m.set_x_parameters(qubit.fr+arange(-0.02e9,0.02e9,0.5e6), 'freq', lambda x: readout.set_tone_freq([x]) and readout.set_LO(x+30e6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qubit.qubit_mw_src.set_status(0)\n",
    "m.comment = 'pseudo vna scan'\n",
    "m.dirname = 'find_fr'\n",
    "m.measure_1D()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data, fn, urls = dr.load_data(None,['freq','pha'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "freq, pha = data\n",
    "pha2 = np.unwrap(pha)\n",
    "pha3 = pha2 - np.linspace(pha2[0],pha2[-1],len(pha2))\n",
    "plt.plot(freq,pha3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pha_gr = np.gradient(pha3)\n",
    "fr = dr.fit_data('dat_import',data_c=1,data=np.array([freq,pha_gr]),nfile=fn)[0][0]*1e9\n",
    "print fr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qubit.fr = fr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qubit.fr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "readout.set_LO(qubit.fr+qubit.readout_iq_frequency)   #set LO higher and use mixer as a down-converter\n",
    "readout.set_tone_freq([qubit.fr])\n",
    "readout.update()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qubit.save()"
   ]
  },
  {
   "cell_type": "heading",
   "metadata": {},
   "level": 3,
   "source": [
    "find transition with homodyne excitation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qubit.f01 = 7e9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qubit.mw_power = 10\n",
    "qubit.qubit_mw_src.set_power(qubit.mw_power)\n",
    "#qubit.update_instruments()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#qubit.update_instruments()\n",
    "qubit.qubit_mw_src.set_status(0)\n",
    "load_awg.update_sequence([qubit.exc_T*.9],gwf.square,qubit)\n",
    "mspec.spec_stop()\n",
    "mspec.set_segments(1)\n",
    "mspec.set_blocks(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mspec.set_averages(1e4)\n",
    "m.set_x_parameters(qubit.f01 + arange(-200e6, 200e6, 1e6), 'f (Hz)', qubit.qubit_mw_src.set_frequency)\n",
    "#m.set_x_parameters(qubit.f01 + arange(-500e6, 500e6, 1e6), 'freq', qubit.qubit_mw_src.set_frequency)\n",
    "#m.set_x_parameters(arange(5.9e9, 7e9, 1e6), 'freq', qubit.qubit_mw_src.set_frequency)\n",
    "m.comment = 'homodyne'\n",
    "m.dirname = 'find_f01'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qubit.qubit_mw_src.set_status(1)\n",
    "m.measure_1D()\n",
    "qubit.qubit_mw_src.set_status(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fq = dr.fit_data(None,ps=[6.97,0.01])[0][0]*1e9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qubit.f01=fq\n",
    "qubit.f01"
   ]
  },
  {
   "cell_type": "heading",
   "metadata": {},
   "level": 1,
   "source": [
    "Heterodyne"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "iq = qkit.instruments.create('iq','IQ_Mixer',sample=qubit,mixer_name='IQ4509')\n",
    "qkit.flow.sleep(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "iq.set_sample(qubit)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qubit.update_instruments()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you have problems with the AWG, e.g. it does not respond with the right values, there is an output in the buffer and you get the response to a previous set command. \n",
    "\n",
    "Execute fastawg.read_queue() until there is no response any longer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fastawg.read_queue()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "load_awg.update_sequence([qubit.exc_T*.9],gwf.square,qubit,iq)\n",
    "mspec.set_segments(1)\n",
    "mspec.set_blocks(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mspec.set_averages(1e4)\n",
    "m.set_x_parameters(qubit.f01 + arange(-80e6, 80e6, 2e6), 'f (Hz)', lambda x: qubit.qubit_mw_src.set_frequency(x-qubit.iq_frequency))\n",
    "m.comment = 'heterodyne'\n",
    "m.dirname = 'f01_hetero'\n",
    "qubit.qubit_mw_src.set_status(1)\n",
    "m.measure_1D()\n",
    "qubit.qubit_mw_src.set_status(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = dr.fit_data(None,ps=[qubit.f01*1e-9,None,None,None])[0][0]*1e9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qubit.f01=f"
   ]
  },
  {
   "cell_type": "heading",
   "metadata": {},
   "level": 3,
   "source": [
    "Heterodyne Rabi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qubit.f01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qubit.update_instruments()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts = np.linspace(0, 0.5, 100)   #in us\n",
    "print '%d Pulses' %(len(ts))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mspec.set_averages(2e3)\n",
    "load_awg.update_sequence(ts*1e-6,gwf.square,qubit,iq)   #load Rabi sequence\n",
    "mspec.spec_stop()\n",
    "mspec.set_segments(len(ts))\n",
    "mspec.set_blocks(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m.time_data = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m.set_x_parameters(ts, 'pulse length (us)', None)\n",
    "qubit.qubit_mw_src.set_status(1)\n",
    "m.comment = 'find pi pulse heterodynely'\n",
    "m.dirname = 'rabi_hetero'\n",
    "qubit.qubit_mw_src.set_status(1)\n",
    "m.measure_1D_AWG(iterations = 20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qubit.set_times(1.0e-6/dr.fit_data(None, fit_function='damped_sine')[0][0]/2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qubit.get_tpi()"
   ]
  },
  {
   "cell_type": "heading",
   "metadata": {},
   "level": 4,
   "source": [
    "T1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qubit.exc_T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts = np.linspace(0, 10, 50)   #in us\n",
    "print '%d Pulses' %(len(ts))\n",
    "#qubit.qubit_mw_src.set_status(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "load_awg.update_sequence(ts*1e-6, gwf.t1, qubit, iq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mspec.set_averages(2e3)\n",
    "mspec.set_segments(len(ts))\n",
    "mspec.set_blocks(1)\n",
    "qubit.qubit_mw_src.set_status(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m.set_x_parameters(ts, 'delay (us)', None)\n",
    "m.comment = 'T1 hetero 10us window %smA, %.3gmK, fq = %s'%(str(vcoil.get_current()),tip.get_T()*1e3,str(qubit.f01))\n",
    "m.dirname = 'T1_hetero_10us'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qubit.qubit_mw_src.set_status(1)\n",
    "m.measure_1D_AWG(iterations = 20)\n",
    "qubit.qubit_mw_src.set_status(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dr.fit_data(None, fit_function='exp')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qubit.save()"
   ]
  },
  {
   "cell_type": "heading",
   "metadata": {},
   "level": 3,
   "source": [
    "Ramsey T2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "qubit.f01 = qubit.f01 + 3e6   #slight detuning\n",
    "qubit.update_instruments()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ts = np.linspace(0, 5, 150)   #in us\n",
    "print '%d Pulses' %(len(ts))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "load_awg.update_sequence(ts*1e-6, gwf.ramsey, qubit, iq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mspec.set_averages(1e3)\n",
    "mspec.set_segments(len(ts))\n",
    "mspec.set_blocks(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m.set_x_parameters(ts, 'delay (us)', None)\n",
    "m.comment = 'Ramsey 5us '+str(qubit.current)\n",
    "m.dirname = m.comment.replace(' ','_')\n",
    "qubit.qubit_mw_src.set_status(1)\n",
    "m.measure_1D_AWG(iterations = 150)\n",
    "qubit.qubit_mw_src.set_status(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dr.fit_data(None, fit_function='damped_sine')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dr.PARAMS[dr.DAMPED_SINE]"
   ]
  }
 ],
 "metadata": {
  "name": "",
  "signature": "sha256:c1ef6526ae92e4b68d259029407f969881d342f9aa4656049e4c2b1825c86f20"
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
